package com.lanou.myim.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.myim.R;
import com.lanou.myim.adapter.SetGroupAdapter;
import com.lanou.myim.presenter.PrivateChatPresenter;
import com.lanou.myim.presenter.PrivateChatPresenterInterface;
import com.parse.ParseObject;

import java.util.List;


/**
 * Created by dllo on 16/3/21.
 */
public class SetGroupActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout relativeLayout;
    private TextView set_id;
    private Intent intent;
    private String chatFriendName;
    private PrivateChatPresenterInterface privateChatPresenter;

    private EditText editText;
    private Button button_add,exit;
    Handler handler;

    private SetGroupAdapter adapter;
    private GridView gridView;
    private List<String> list;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_group);

        relativeLayout = (RelativeLayout) findViewById(R.id.set_group);
        exit = (Button) findViewById(R.id.exit);
        set_id = (TextView) findViewById(R.id.set_id);
        gridView = (GridView) findViewById(R.id.gridview);
        relativeLayout.setOnClickListener(this);
        intent = getIntent();
        chatFriendName = intent.getStringExtra("groupId");
        set_id.setText(chatFriendName);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                Toast.makeText(getApplicationContext(),message.getData().get("msg").toString(),Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        editText = (EditText) findViewById(R.id.edittext);

        button_add = (Button) findViewById(R.id.button_add);
        button_add.setOnClickListener(this);


        EMChatManager.getInstance().getChatOptions().setUseRoster(true);
        try {
            list = EMContactManager.getInstance().getContactUserNames();
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
        adapter = new SetGroupAdapter(this,list);
        gridView.setAdapter(adapter);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.set_group:
                privateChatPresenter.clearMsgs(chatFriendName);
                break;
            case R.id.button_add:
                //群主加人调用此方法
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Message msg = new Message();
                        Bundle bundle = new Bundle();
                        try {
                            String[] adduser = new String[]{editText.getText().toString()};
                            EMGroupManager.getInstance().addUsersToGroup(intent.getStringExtra("groupId"), adduser);//需异步处理
                            bundle.putString("msg","已发送邀请");
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        } catch (EaseMobException e) {
                            e.printStackTrace();
                            bundle.putString("msg",e.getMessage());
                            msg.setData(bundle);
                            handler.sendMessage(msg);
                        }
                    }
                });


                break;

            case R.id.exit:



                break;



        }


    }
}
